/**
 * Created by P0084519 on 12/6/2017.
 */
import constants from './constants'

const getMOCK = () => {
    return {
        environment: 'MOCK',
        services: {
            customService: {
                serviceName: 'customService',
                identifier: '',
                url: {
                  getPostsData: '../static/mock/getPostsData.json'
                }
            }
        }
    };
};

const getUAT = () => {
    return {
        environment: 'UAT',
        services: {
            customService: {
                serviceName: 'customService',
                identifier: '',
                url: {
                  getPostsData: 'http://jsonplaceholder.typicode.com/posts',
                }
            }
        }
    };
};

const getSIT = () => {
    return {
        environment: 'SIT',
        services: {
            customService: {
                serviceName: 'customService',
                identifier: 'http://jsonplaceholder.typicode.com'
            }
        }
    };
};

const getPRO = () => {
    return {
        environment: 'PRO',
        services: {
            customService: {
                serviceName: 'customService',
                identifier: 'http://jsonplaceholder.typicode.com'
            }
        }
    };
};

var _basicUrls = {
  getPostsData: '/posts',
};

const _joinUrl = (domain, path) => {
    return domain.replace(/(^http.*?)\/{0,}$/, '$1/') + path.replace(/^\/{0,}(.*)/, '$1');
};

const _generateURLs = (identifier, urls) => {
    // var newUrls = $.extend(true, {}, urls);
    let newUrls = JSON.parse(JSON.stringify(urls));

    for (let key in newUrls) {
        let v = newUrls[key], k = key;
        if (typeof v === 'string') {
            if (!(v.match(/^http/))) {
                newUrls[k] = _joinUrl(identifier, v);
            }
        }
        else {
            newUrls[k] = _generateURLs(identifier, v);
        }
    }

    return newUrls;
};

let config = {};
let environment = {
    getEnvironment: () => {
        return config;
    },
    setEnvironment: (env) => {
        let tempConfig = {};
        if (env === constants.ENVS.MOCK) {
            tempConfig = getMOCK();
        }
        else if (env === constants.ENVS.SIT) {
            tempConfig = getSIT();
        }
        else if (env === constants.ENVS.UAT) {
            tempConfig = getUAT();
        }
        else if (env === constants.ENVS.PRO) {
            tempConfig = getPRO();
        }
        if (!tempConfig.services.customService.url) {
            tempConfig.services.customService.url = _generateURLs(tempConfig.services.customService.identifier, _basicUrls);
        }
        if (!tempConfig.services.customService.urlParams) {
            if (env === constants.ENVS.MOCK) {
                tempConfig.services.customService.urlParams = tempConfig.services.customService.url;
            } else {
                tempConfig.services.customService.urlParams = _basicUrls;
            }
        }

        config = tempConfig;
    }
};

export default environment;
